<?php
if (!session_id()) {
    session_start();
}
/**
 * Copyright 2011 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
require 'src/facebook.php';
global $wpdb;

  // Create our Application instance (replace this with your appId and secret).
  $facebook = new Facebook(array(
  'appId' => '185856284953433',
  'secret' => 'a064b22d7010b7483c90ae7d5067b668',
  'cookie' => true
  ));
  
  $secret = 'a064b22d7010b7483c90ae7d5067b668'; 
  /*
  
$signed_request = $facebook->getSignedRequest(); 
//https://www.facebook.com/www.ebom.vn/app_185856284953433?app_data={%22a%22:1,%22b%22:2,%22c%22:3,%22d%22:4,%22e%22:5}
$app_data = json_decode($signed_request['app_data']); 
print_r($app_data); die('hehe'); 
if(isset($app_data->imgid)){
	$img_url = home_url('carddetail').'/?imgid='.$app_data->imgid;
	wp_redirect($img_url);
	die; 
}
*/
  if(isset($_SESSION['user_id']))
  $xuser = $wpdb->get_row("SELECT * FROM ap_painting_user WHERE  user_id = {$_SESSION['user_id']}");

  if(!(isset($_SESSION['user_id']) && $_SESSION['user_id']) || (isset($_SESSION['user_id']) && !isset($xuser->id) )){


  // Get User ID
  $user = $facebook->getUser();


  // We may or may not have this data based on whether the user is logged in.
  //
  // If we have a $user id here, it means we know the user is logged into
  // Facebook, but we don't know if the access token is valid. An access
  // token is invalid if the user logged out of Facebook.

  if ($user) {
  try {
  // Proceed knowing you have a logged in user who's authenticated.
  $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
  error_log($e);
  $user = null;
  }
  } else {
  $loginUrl = $facebook->getLoginUrl();
  echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
  exit;
  }

  $_SESSION['user_id'] = $user;
  $_SESSION['username'] = $user_profile['name'];
  $xuser = $wpdb->get_row("SELECT * FROM ap_painting_user WHERE  user_id = {$_SESSION['user_id']}");
  if(!isset($xuser->id)){
  global $wpdb;
  $tb = 'ap_painting_user' ;
  $data = array('name'=> $_SESSION['username'], 'user_id' => $_SESSION['user_id'] );
  $wpdb->insert($tb,$data,array('%s', '%s'));
  }else{
  global $wpdb;
  $tb = 'ap_painting_user' ;
  $data = array('name'=> $_SESSION['username'], 'user_id' => $_SESSION['user_id'] );
  $wpdb->update($tb,$data,array( 'id' => $xuser->id ),array('%s', '%s'));
  }
  }

  $xuser = $wpdb->get_row("SELECT * FROM ap_painting_user WHERE  user_id = {$_SESSION['user_id']}");

  if(!isset($xuser->id) && !is_home()){
  wp_redirect( home_url() ); exit;
  }

  /*
  if (is_home() && $_SESSION['user_id'] && 0 ) {
  $location = get_site_url() . '/the-le';
  wp_redirect($location);
  } */
  
  
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="vi" xml:lang="vi">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <meta charset="<?php bloginfo('charset'); ?>" />

        <title> <?php wp_title('|', true, 'right'); ?></title>
        <script type="text/javascript" src="<?php echo bloginfo('template_url'); ?>/js/jquery.js"></script>
        <script type="text/javascript" src="<?php echo bloginfo('template_url'); ?>/js/jquery-ui.js"></script>
        <script type="text/javascript" src="<?php echo bloginfo('template_url'); ?>/js/slimScroll.js"></script>
        <script type="text/javascript" src="<?php echo bloginfo('template_url'); ?>/js/snow.js"></script>
        <link href="<?php echo bloginfo('template_url'); ?>/css/style.css" type="text/css" rel="stylesheet" />
        <script type="text/javascript" src="<?php echo bloginfo('template_url'); ?>/js/jquery.blockUI.js"></script>
        <link href="<?php echo bloginfo('template_url'); ?>/css/custom.css" type="text/css" rel="stylesheet" />
        <link href="<?php echo bloginfo('template_url'); ?>/css/animation.css" type="text/css" rel="stylesheet" />

        <?php //wp_head();    ?>

        <script type="text/javascript">

            var base_url = '<?php echo bloginfo('template_url'); ?>/';
			var site_url = '<?php echo site_url() ?>';
        function setSession(value){
				$.ajax({
			        type: "GET",
			        url: site_url,
			        data: {
			            music: value,
			        }
			    });
        }
		function setCookie(c_name,value)
		{		exdays = 10; 
				var exdate=new Date();
				exdate.setDate(exdate.getDate() + exdays);
				var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
				document.cookie=c_name + "=" + c_value;
				console.log(document.cookie);
		}
		function getCookie(c_name)
		{
				var c_value = document.cookie;
				var c_start = c_value.indexOf(" " + c_name + "=");
				if (c_start == -1)
				  {
				  c_start = c_value.indexOf(c_name + "=");
				  }
				if (c_start == -1)
				  {
				  c_value = null;
				  }
				else
				  {
				  c_start = c_value.indexOf("=", c_start) + 1;
				  var c_end = c_value.indexOf(";", c_start);
				  if (c_end == -1)
				  {
				c_end = c_value.length;
				}
				c_value = unescape(c_value.substring(c_start,c_end));
				}
				return c_value;
		}
        </script>
    </head>

    <body>
        <?php
        $page_class = '';
        if (is_home()) {
            $page_class = 'intro';
        } else if (is_page('album')) {
            $page_class = 'album';
        }
        ?>
        <div class="container <?php echo $page_class ?>">
			<div class="centerclass">
            <div class="header">
                <img class="noel" src="<?php echo bloginfo('template_url'); ?>/img/noel.png">
                <h4 class="hthe-le">Thắp sáng mùa Lễ hội</h4>
                <a href="<?php echo site_url() ?>"><img class="logo1" src="<?php echo bloginfo('template_url'); ?>/img/cream.png"></a>
            </div>
            <div class="main">